home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / DVIM72-Mac 1.9.6 / source / mac-specific.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-14  |  33.4 KB  |  1,353 lines  |  [TEXT/R*ch]

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <PrintTraps.h>
  6. #include <Balloons.h>
  7. #include "mac-specific.h"
  8. #include "mac_printing.h"
  9. #include "dvihead.h"
  10. #include "gendefs.h"
  11. #include "egblvars.h"
  12. #include "gblprocs.h"
  13. #include "m72.h"
  14. #include "TextDisplay.h"
  15. #include "sillymalloc.h"
  16. #include "Copy_banded.h"
  17.  
  18. #define PROFILE 0
  19.  
  20. #define REVEAL_DIALOG(d) {SelectWindow(d);ShowWindow(d);}
  21. #define MOVE_TO_FRONT        -1L
  22. #define MIN_SLEEP            3L
  23. #define NIL_MOUSE_REGION    0L
  24. #define NIL_POINTER            0L
  25. #define GROW_ICON_SIZE        16
  26.  
  27. #define WNE_TRAP_NUM        0xA860
  28.  
  29. #define APPLE_MENU_ID        1
  30. #define FILE_MENU_ID        401
  31.  
  32. #define HELP_ID            128
  33.  
  34. #define ABOUT_ITEM            1
  35. #define APPLE_HELP_ITEM        2
  36.  
  37. #define DRAG_THRESHOLD    30
  38. #define IDLE_INTERVAL    10    /* Min. interval in ticks between printer idle stuff */
  39.  
  40. /* ------------- global variables ----------------- */
  41. DialogPtr            g_cancel_dialog;
  42. Handle            cancel_dialog_text;
  43. EventRecord        g_TheEvent;
  44. Rect                 g_DragRect, g_screenrect;
  45. MenuHandle        g_AppleMenu, g_FileMenu;
  46. Boolean            g_Done, g_WNEImplemented;
  47. Boolean            g_abort_dvi; /* error cancels this dvi file */
  48. int                g_dpi;    /* bitmap resolution */
  49. int                g_printer_dpi;    /* device resolution */
  50. int                g_print_res_file;
  51. short                g_app_resfile;
  52. GrafPort        g_offscreen_GrafPort;
  53. Boolean        g_draw_offscreen;
  54. Boolean        g_preview;    /* use preview window */
  55. WindowPtr        g_page_window,
  56.                 g_console_window;
  57. THPrint            g_print_rec_h = NIL_POINTER;
  58. Str255            g_other_commands;
  59. WindowPtr        g_freemem_window;
  60. MenuHandle        g_popup_menu;
  61. Boolean            g_use_sysheap;
  62. long                g_last_idle_time = 0L;
  63. Boolean            g_help_under_apple;
  64. short                g_paper_width;
  65. short                g_paper_length;
  66. QDProcs            offscreen_QDprocs;
  67. short                g_print_status; // 1=driver open, 2=doc open, 3=page open
  68.  
  69. /* -------------- prototypes of private routines ------------ */
  70. void MainLoop( void );
  71. void HandleMouseDown( void );
  72. void HandleAppleChoice( short theItem );
  73. void    show_about        ( void );
  74. void    Show_warning(     char *msg );
  75. void    init_toolbox    ( void );
  76. int        Get_resource_id(
  77.                         OSType rsrc_type,
  78.                         char *rsrc_name);
  79. Handle Get_resource_by_id( OSType rsrc_type, int r_id );
  80. void    Get_resource(                /* find a resource by name */
  81.                         OSType resource_type,
  82.                         char *resource_name,
  83.                         Handle *resource_handle,
  84.                         short *resource_id);
  85. void    Get_file_path(     char *thepath );
  86. Boolean    User_wants_out    ( void );
  87. void    init_per_dvi(    void );
  88. pascal Boolean
  89.         Dvi_filter(     ParmBlkPtr param_block );
  90. void    Get_dpi(        void );
  91. Boolean Get_event(        int mask,
  92.                         EventRecord *what);
  93. void    Get_dialogs(    void );
  94. void    Setup_drag_rect(    void );
  95. pascal Boolean    Do_update( WindowPtr  where );
  96. void    Handle_print(    Boolean use_job_dialog );
  97. void HandleMenuChoice( long menuChoice );
  98. void    Handle_event( void );
  99. void    Do_page_setup(    void );
  100. void    Init_print_rec(    void );
  101. void    Open_printer(    Boolean use_job_dialog,
  102.                         int *first,
  103.                         int *last );
  104. void    HandleFileChoice( int theItem );
  105. Boolean    IsDAWindow( WindowPtr whichWindow );
  106. Boolean    get_bool_resource( char *rsrc_name );
  107. int        get_int_resource( char *rsrc_name );
  108. void        set_int_resource( char *rsrc_name, int new_value );
  109. void    set_str_resource(
  110.                         char *rsrc_name,
  111.                         char *new_value );
  112. char **
  113.         get_str_resource( char *rsrc_name );
  114. pascal void    Printer_idle( void );
  115. void Update_PREC( void );
  116. void  Set_pic_procs( void );
  117. void    Do_help( void );
  118. void Init_menubar( void );
  119. void Draw_grow_icon( void );
  120.  
  121. /* -------------------------------------------------------------------- */
  122.  
  123. /* ----- a couple of prototypes of external routines ------ */
  124. void    Handle_settings( void );
  125. void    dvifile( char *filename );
  126. void    option( char *an_option );
  127. void    rollbeachball( void );
  128. Boolean setupbeachball( void );
  129. void    Reposition_windows( void );
  130. void    Save_window_positions( void );
  131. void    Center_window(    WindowPtr my_window );
  132. void    Explain_print_error( OSErr the_error );
  133. void    Update_freemem( void );
  134. void    Update_memory_indicators( void );
  135. void    openfont( char *fontname ); /* only used here for UnloadSeg */
  136. void    special( char *s ); /* only used here for UnloadSeg */
  137. ControlHandle    Get_Ditem_handle( DialogPtr theDialog, int item_no );
  138. pascal void Get_PICT_data( Ptr data_ptr, int byte_count );
  139. void    Install_help( void );
  140. void Maximize_resolution( void );
  141. void Reset_resolution( void );
  142. void Find_prefs( void );
  143. Boolean Cancel_key( EventRecord *theEvent );
  144. Boolean TrapAvailable( short trapnum );
  145.  
  146. /* ----------------------------- main ------------------------------ */
  147. void
  148. main()
  149. {
  150.     init_toolbox();
  151.     (void) strcpy(g_progname, "DVIM72-Mac");
  152.     g_app_resfile = CurResFile();
  153.     Setup_drag_rect();
  154.     EventAvail( everyEvent, &g_TheEvent ); /* Move to the foreground */
  155.     EventAvail( everyEvent, &g_TheEvent ); /* Move to the foreground */
  156.     EventAvail( everyEvent, &g_TheEvent ); /* Move to the foreground */
  157.     Find_prefs();
  158.     Get_dialogs();
  159.     Reposition_windows();
  160.     printf( "\n\n\n\nWelcome to DVIM72-Mac.\n" ); CHK("begin");
  161.     REVEAL_DIALOG( g_freemem_window );
  162.     silly_get_MF_mem_status();
  163.     g_use_sysheap = MF_mem_status >= sm_temp_mem;
  164.     g_print_status = 0;
  165.     Get_dpi();
  166.     (void) initglob();
  167.     if (g_preview)
  168.         REVEAL_DIALOG( g_page_window );
  169.     Init_menubar();
  170.     
  171.     /*
  172.         Offscreen drawing initialization
  173.     */
  174.     g_draw_offscreen = get_bool_resource("DRAW OFFSCREEN");
  175.     g_print_by_bands = get_bool_resource("BANDING") && g_draw_offscreen;
  176.     OpenPort( &g_offscreen_GrafPort );
  177.     CopyRgn( g_offscreen_GrafPort.clipRgn, g_offscreen_GrafPort.visRgn ); /* wide open */
  178.     g_offscreen_GrafPort.portBits.baseAddr = NIL;
  179.     SetStdProcs( &offscreen_QDprocs );
  180.     offscreen_QDprocs.getPicProc = (Ptr) &Get_PICT_data;
  181.     g_offscreen_GrafPort.grafProcs = &offscreen_QDprocs;
  182.  
  183.     Update_freemem();
  184.     InitCursor();
  185.     UseResFile( g_app_resfile );
  186.     (void) setupbeachball(); CHK("before main loop");
  187.     UseResFile( g_pref_resfile );
  188.     MainLoop();
  189.     Save_window_positions();
  190.     TD_close_log();
  191. }
  192.  
  193.  
  194. /* ------------------------ Setup_drag_rect ----------------- */
  195. void
  196. Setup_drag_rect()
  197. {
  198.     SysEnvRec    the_world;
  199.     OSErr             err;
  200.  
  201.     err = SysEnvirons(1, &the_world );
  202.     if ( (err != noErr) || !the_world.hasColorQD )
  203.     {
  204.         g_screenrect = screenBits.bounds;
  205.     }
  206.     else
  207.         g_screenrect = (**GetMainDevice()).gdRect;
  208.     if (EmptyRect(&g_screenrect))
  209.         fatal("Can't get the screen rectangle!\n");
  210.     g_screenrect.top += GetMBarHeight();
  211.     g_DragRect = (**GetGrayRgn()).rgnBBox;
  212.     InsetRect( &g_DragRect, DRAG_THRESHOLD, DRAG_THRESHOLD );
  213.     g_DragRect.top -= DRAG_THRESHOLD;
  214. }
  215.  
  216. /* ------------------- Set_pic_procs ------------------------ */
  217. /*
  218.     If we are not drawing offscreen, set the getPicProc in the printing GrafPort
  219.     and in the page preview window, in order to draw pictures from PICT files.
  220. */
  221. void  Set_pic_procs( void )
  222. {
  223.     GrafPtr save_port;
  224.     QDProcsPtr    my_procs;
  225.     
  226.     GetPort( &save_port );
  227.     SetPort( (GrafPtr)g_print_port_p );
  228.     if (g_print_port_p->gPort.grafProcs == NIL)
  229.     {
  230.         /*
  231.             One case in which this happens is when the StyleWriter driver
  232.             has background printing turned on.  Then PrOpenPage just does an
  233.             OpenPicture.
  234.         */
  235.         my_procs = (QDProcsPtr) MALLOC( sizeof(QDProcs) );
  236.         if (my_procs != NIL)
  237.         {
  238.             SetStdProcs( my_procs );
  239.             my_procs->getPicProc = (Ptr)&Get_PICT_data;
  240.             g_print_port_p->gPort.grafProcs = my_procs;
  241.         }
  242.     }
  243.     else
  244.         g_print_port_p->gPort.grafProcs->getPicProc = (Ptr)&Get_PICT_data;
  245.     
  246.     SetPort( g_page_window );
  247.     if (g_page_window->grafProcs == NIL)
  248.     {
  249.         my_procs = (QDProcsPtr) MALLOC( sizeof(QDProcs) );
  250.         if (my_procs != NIL)
  251.         {
  252.             SetStdProcs( my_procs );
  253.             my_procs->getPicProc = (Ptr)&Get_PICT_data;
  254.             g_page_window->grafProcs = my_procs;
  255.         }
  256.     }
  257.     SetPort( save_port );
  258. }
  259.  
  260. /* ---------------------------- Get_dialogs ------------------------- */
  261. void
  262. Get_dialogs()
  263. {
  264.     GrafPtr save_port;
  265.     int cancel_DLOG_id;
  266.     int WIND_id;
  267.     OSErr    stat;
  268.     
  269.     GetPort( &save_port );
  270.  
  271.     cancel_DLOG_id = Get_resource_id( 'DLOG', "cancel print" );
  272.     CouldDialog( cancel_DLOG_id );
  273.     g_cancel_dialog = GetNewDialog( cancel_DLOG_id,
  274.         nil, (WindowPtr)MOVE_TO_FRONT );
  275.     cancel_dialog_text = (Handle)Get_Ditem_handle( g_cancel_dialog, 1 );
  276.     
  277.     WIND_id = Get_resource_id( 'WIND', "console" );
  278.     g_console_window = TD_new( WIND_id, 5000,  4000, 9, &stat );
  279.     if (stat != noErr)
  280.         fatal("Error opening console window.");
  281.  
  282.     WIND_id = Get_resource_id( 'WIND', "FreeMem" );
  283.     g_freemem_window = GetNewWindow( WIND_id,
  284.         nil, (WindowPtr)MOVE_TO_FRONT );
  285.  
  286.     g_preview = get_bool_resource("PREVIEW");
  287.     WIND_id = Get_resource_id( 'WIND', "page" );
  288.     g_page_window = GetNewWindow( WIND_id,
  289.         nil, (WindowPtr)MOVE_TO_FRONT );
  290.     
  291.     SetPort( save_port );
  292. }
  293.  
  294. /* --------------------------- Get_dpi ----------------------------- */
  295. void
  296. Get_dpi()
  297. {
  298.     g_dpi = get_int_resource("BITMAP DPI");
  299. }
  300.  
  301.  
  302. /* ------------------------ MainLoop ------------------- */
  303.  
  304. void MainLoop( void )
  305. {
  306.     g_Done = FALSE;
  307.     g_WNEImplemented = TrapAvailable( WNE_TRAP_NUM );
  308.     while ( g_Done == FALSE )
  309.     {
  310.         Handle_event();
  311.     }
  312.     /* fflush(stdout); */
  313. }
  314.  
  315. #define    RESUME_MASK            1
  316. #define    HELP_CHAR            0x05
  317. /* ----------------- Handle_event ----------------- */
  318.  
  319. void Handle_event()
  320. {
  321.     char theChar;
  322.  
  323.     (void)Get_event( everyEvent, &g_TheEvent );
  324.     
  325.     switch (g_TheEvent.what)
  326.     {
  327. #if NEVER
  328.         case nullEvent:
  329.             /* HandleNull(); */
  330.             break;
  331. #endif
  332.         case mouseDown:
  333.             HandleMouseDown();
  334.             break;
  335.         case keyDown:
  336.         case autoKey:
  337.             theChar = g_TheEvent.message & charCodeMask;
  338.             if (Cancel_key(&g_TheEvent)
  339.                 && ((WindowPeek)g_cancel_dialog)->visible )
  340.             {
  341.                 g_abort_dvi = TRUE;
  342.                 PrSetError( iPrAbort );
  343.                 SetIText( cancel_dialog_text, "\pCancelling..." );
  344.                 SetPort( g_cancel_dialog );
  345.                 DrawDialog( g_cancel_dialog );
  346.             }
  347.             else if ( (g_TheEvent.modifiers & cmdKey) != 0 )
  348.                 HandleMenuChoice( MenuKey( theChar ) );
  349.             else if (theChar == HELP_CHAR)
  350.                 Do_help();
  351.             else
  352.                 SysBeep(1);
  353.             break;
  354.         case updateEvt:
  355.             Do_update( (WindowPtr)g_TheEvent.message );
  356.             break;
  357.         case activateEvt:
  358.             SetPort( (WindowPtr)g_TheEvent.message );
  359.             if (thePort == g_console_window)
  360.             {
  361.                 if (g_TheEvent.modifiers & activeFlag) /* activate event */
  362.                     TD_activate( TRUE );
  363.                 else
  364.                     TD_activate( FALSE );
  365.                 Draw_grow_icon();
  366.             }
  367.             break;
  368.         case app4Evt:
  369.             SetPort( FrontWindow() );
  370.             if (g_TheEvent.message & RESUME_MASK)
  371.             {
  372.                 TD_activate( TRUE );
  373.                 Update_memory_indicators();
  374.                 InitCursor();
  375.             }
  376.             else
  377.                 TD_activate( FALSE );
  378.             break;
  379.     }
  380. }
  381.  
  382. /* ------------------- Do_Update -------------------------- */
  383. pascal Boolean
  384. Do_update( where )
  385.     WindowPtr where;
  386. {
  387.     Rect    from_rect;
  388.     GrafPtr    save_port;
  389.     
  390.     if ( !IsDAWindow( where ) )
  391.     {
  392.         GetPort( &save_port );
  393.         SetPort( (GrafPtr)where );
  394.         BeginUpdate( where );
  395.         if (where == g_page_window)
  396.         {
  397.             DrawGrowIcon( where );
  398.         }
  399.         else if (where == g_cancel_dialog)
  400.             DrawDialog( g_cancel_dialog );
  401.         else if (where == g_console_window)
  402.         {
  403.             EraseRect( &where->portRect );
  404.             Draw_grow_icon();
  405.             DrawControls( where );
  406.             TD_update( );
  407.         }
  408.         EndUpdate( where );
  409.         /*
  410.             Update the free memory window outside BeginUpdate--EndUpdate
  411.             in order to redraw the whole thing.  Otherwise the numbers get
  412.             "fractured".
  413.         */
  414.         if (where == g_freemem_window)
  415.             Update_freemem();
  416.         SetPort( save_port );
  417.     }
  418.     return true;
  419. }
  420.  
  421. /* ----------------- Draw_grow_icon ----------------- */
  422. /*
  423.     Draw the grow icon with clipping so that it does not draw scroll bar lines.
  424. */
  425. void Draw_grow_icon( void )
  426. {
  427.     Rect icon_rect;
  428.     RgnHandle    save_clip;
  429.     
  430.     icon_rect = thePort->portRect;    /* get right and bottom */
  431.     icon_rect.top = icon_rect.bottom - GROW_ICON_SIZE + 1;
  432.     icon_rect.left = icon_rect.right - GROW_ICON_SIZE + 1;
  433.     
  434.     save_clip = NewRgn();
  435.     GetClip( save_clip );
  436.     ClipRect( &icon_rect );
  437.     DrawGrowIcon( thePort );
  438.     SetClip( save_clip );
  439.     DisposeRgn( save_clip );
  440. }
  441.  
  442.  
  443. /* ----------------- HandleMouseDown ----------------- */
  444.  
  445. void HandleMouseDown( void )
  446. {
  447.     WindowPtr    whichWindow;
  448.     int            thePart;
  449.     long        menuChoice, windSize;
  450.     long new_size;
  451.     
  452.     thePart = FindWindow( g_TheEvent.where, &whichWindow );
  453.     switch (thePart)
  454.     {
  455.         case inMenuBar:
  456.             menuChoice = MenuSelect( g_TheEvent.where );
  457.             HandleMenuChoice( menuChoice );
  458.             break;
  459.         case inSysWindow:
  460.             SystemClick( &g_TheEvent, whichWindow );
  461.             break;
  462.         case inDrag:
  463.             SelectWindow( whichWindow );
  464.             DragWindow( whichWindow, g_TheEvent.where, &g_DragRect );
  465.             break;
  466.         case inContent:
  467.             SelectWindow( whichWindow );
  468.             SetPort( whichWindow );
  469.             if (whichWindow == g_console_window)
  470.                 TD_click(g_TheEvent.where);
  471.             else
  472.                 DragWindow( whichWindow, g_TheEvent.where, &g_DragRect );
  473.             break;
  474.         case inGrow:
  475.             SelectWindow( whichWindow );
  476.             new_size = GrowWindow( whichWindow, g_TheEvent.where,
  477.                 &g_DragRect );
  478.             if (new_size)
  479.             {
  480.                 SizeWindow( whichWindow, LoWord(new_size),
  481.                     HiWord(new_size), TRUE );
  482.                 SetPort( whichWindow );
  483.                 if (whichWindow != g_console_window)
  484.                     InvalRect( &whichWindow->portRect );
  485.                 else
  486.                     TD_resize();
  487.             }
  488.     }
  489. }
  490.  
  491. /* -------------------- HandleMenuChoice ----------------------- */
  492.  
  493. void HandleMenuChoice( long menuChoice )
  494. {
  495.     int  theMenu, theItem;
  496.     
  497.     if ( menuChoice != 0 )
  498.     {
  499.         theMenu = HiWord( menuChoice );
  500.         theItem = LoWord( menuChoice );
  501.         switch( theMenu )
  502.         {
  503.             case APPLE_MENU_ID:
  504.                 HandleAppleChoice( theItem );
  505.                 break;
  506.             case FILE_MENU_ID:
  507.                 HandleFileChoice( theItem );
  508.                 break;
  509.             case kHMHelpMenuID:
  510.                 Do_help();
  511.         }
  512.     }
  513.     HiliteMenu( 0 );
  514. }
  515.  
  516. /* --------------------- Do_help ------------------------- */
  517. void    Do_help( void )
  518. {
  519.     pascal void (*Show_help)( short help_id, 
  520.         pascal Boolean (*Update_proc)(WindowPtr) );
  521.     Handle    help_CUST;
  522.     short        save_resfile;
  523.     
  524.     save_resfile = CurResFile();
  525.     UseResFile( g_app_resfile );
  526.     help_CUST = Get1NamedResource( 'CUST', "\pShow_help" );
  527.     HLock( help_CUST );
  528.     Show_help = (pascal void (*)(short, pascal Boolean (*Update_proc)(WindowPtr)))
  529.                 StripAddress( *help_CUST );
  530.     (*Show_help)( HELP_ID, Do_update );
  531.     UseResFile( save_resfile );
  532.     HUnlock( help_CUST );
  533. }
  534.  
  535.  
  536. /* ------------------- HandleAppleChoice ------------------- */
  537.  
  538. void HandleAppleChoice( short theItem )
  539. {
  540.     Str255        accName;
  541.     short            accNumber;
  542.     
  543.     switch( theItem )
  544.     {
  545.         case ABOUT_ITEM:
  546.             show_about();
  547.             break;
  548.         default:
  549.             if (g_help_under_apple && (theItem == APPLE_HELP_ITEM))
  550.                 Do_help();
  551.             else
  552.             {
  553.                 GetItem( g_AppleMenu, theItem, accName );
  554.                 accNumber = OpenDeskAcc( accName );
  555.             }
  556.             break;
  557.     }
  558. }
  559.  
  560. #define USE_JOB_DIALOG    1
  561. #define NO_JOB_DIALOG    0
  562. /* --------------------- HandleFileChoice ------------------- */
  563. typedef enum {
  564.     PAGE_SETUP_ITEM = 1,
  565.     SETTINGS_ITEM,
  566.     /* ---- */
  567.     PRINT_ITEM = 4,
  568.     JUST_PRINT_ITEM,
  569.     /* ----- */
  570.     QUIT_ITEM = 7
  571. };
  572.  
  573. void
  574. HandleFileChoice( theItem )
  575. int    theItem;
  576. {
  577.     switch( theItem )
  578.     {
  579.         case PAGE_SETUP_ITEM:
  580.             Do_page_setup();
  581.             break;
  582.         case SETTINGS_ITEM:
  583.             Handle_settings();
  584.             UnloadSeg( Handle_settings );
  585.             break;
  586.         case PRINT_ITEM:
  587.             Handle_print( USE_JOB_DIALOG );
  588.             break;
  589.         case JUST_PRINT_ITEM:
  590.             Handle_print( NO_JOB_DIALOG );
  591.             break;
  592.         case QUIT_ITEM:
  593.             g_Done = TRUE;
  594.             break;
  595.     }
  596. }
  597.  
  598.  
  599. /* ---------------------- IsDAWindow ------------------------ */
  600.  
  601. Boolean    IsDAWindow( WindowPtr whichWindow )
  602. {
  603.     if ( whichWindow == NIL_POINTER )
  604.         return( FALSE );
  605.     else
  606.         /* DA windows have negative WindowKinds  */
  607.         return( ( ((WindowPeek)whichWindow) -> windowKind ) < 0 );
  608. }
  609.  
  610.  
  611. /* ------------------- Get_event ------------------ */
  612. Boolean
  613. Get_event( int event_mask, EventRecord *what_event )
  614. {
  615.     register  Boolean val;
  616.     
  617.     if ( g_WNEImplemented )
  618.         val = WaitNextEvent( event_mask, what_event, MIN_SLEEP,
  619.             NIL_MOUSE_REGION );
  620.     else
  621.     {
  622.         SystemTask();
  623.         val = GetNextEvent( event_mask, what_event );
  624.     }
  625.     return( val );
  626. }
  627.  
  628.  
  629. /* ----------------------- Init_menubar ------------------- */
  630.  
  631. void Init_menubar( void )
  632. {
  633.     int MBAR_id;
  634.     Handle my_bar;
  635.     int popup_id;
  636.     
  637.     MBAR_id = Get_resource_id( 'MBAR', "bar" );
  638.     my_bar = GetNewMBar( MBAR_id );
  639.     SetMenuBar( my_bar );
  640.     g_AppleMenu = GetMHandle( APPLE_MENU_ID );
  641.     g_FileMenu = GetMHandle( FILE_MENU_ID );
  642.     AddResMenu( g_AppleMenu, 'DRVR' );
  643.     Install_help();
  644.     DrawMenuBar();
  645.     
  646.     popup_id = Get_resource_id( 'MENU', "popup" );
  647.     g_popup_menu = GetMenu( popup_id );
  648.     InsertMenu( g_popup_menu, -1 );
  649. }
  650.  
  651. /* --------------------- Handle_print --------------------- */
  652. void
  653. Handle_print( Boolean use_job_dialog )
  654. {
  655.     char    t_str[256], t_str2[256];
  656.     char    dvi_file[256];
  657.     char    option_string[256];
  658.     char    *t_ptr;
  659.     int        first_page, last_page;
  660.     
  661.     g_abort_dvi = FALSE;
  662.     Get_file_path( dvi_file );
  663.     if (strlen(dvi_file) == 0)        /* user chose "cancel" in SF dialog */
  664.         return;
  665.     
  666.     /* Initialize defaults */
  667.     init_per_dvi();
  668.     
  669.     Open_printer( use_job_dialog, &first_page, &last_page );
  670.     if (g_abort_dvi)
  671.     {
  672.         InitCursor(); /* in case of beach ball */
  673.         return;  /* Error or else user clicked Cancel */
  674.     }
  675.     
  676.     /* Get info out of options dialog send to option() */
  677.     if ( (first_page != 1) || (last_page != iPrPgMax) )
  678.     {
  679.         (void)sprintf( t_str, "%d", first_page );
  680.         (void)sprintf( t_str2, "%d", last_page );
  681.         (void) strcpy(option_string, "-o");
  682.         (void) strcat( option_string, t_str );
  683.         (void) strcat( option_string, ":" );
  684.         (void) strcat( option_string, t_str2 );
  685.         option( option_string );
  686.     }
  687.     rollbeachball();
  688.     (void) PtoCstr( g_other_commands );
  689.     if (strlen((char *)g_other_commands))
  690.     {
  691.         t_ptr = strtok( (char *)g_other_commands, " \t" );
  692.         while( t_ptr != NULL )
  693.         {
  694.             option( t_ptr );
  695.             t_ptr = strtok( NULL, " \t" );
  696.         }
  697.     }
  698.     
  699.     /* Send offset info from config to option() */
  700.     t_ptr = *get_str_resource( "HOFFSET" );
  701.     if (strlen(t_ptr))
  702.     {
  703.         (void)strcpy( option_string, "-x" );
  704.         (void)strcat( option_string, t_ptr );
  705.         option( option_string );
  706.     }
  707.     t_ptr = *get_str_resource( "VOFFSET" );
  708.     if (strlen(t_ptr))
  709.     {
  710.         (void)strcpy( option_string, "-y" );
  711.         (void)strcat( option_string, t_ptr );
  712.         option( option_string );
  713.     }
  714.     
  715.     if (npage == 0)        /* no page ranges given, make a large one */
  716.     {
  717.         page_begin[0] = 1;
  718.         page_end[0] = 32767;    /* arbitrary large integer */
  719.         page_step[0] = 1;
  720.         npage = 1;
  721.     }
  722.  
  723.     if (! g_abort_dvi)
  724.     {
  725.         SetPort( g_cancel_dialog );
  726.         DisableItem( g_FileMenu, 0 );    /* disable entire File menu */
  727.         DrawMenuBar();
  728.         SetPort( (GrafPtr) g_print_port_p );
  729.         dvifile( dvi_file );    /* go print it! */;
  730.         SetPort( FrontWindow() );
  731.         InitCursor();
  732.         Update_memory_indicators();
  733.         EnableItem( g_FileMenu, 0 );    /* enable entire File menu */
  734.         DrawMenuBar();
  735.     }
  736.     else
  737.         Close_printer();
  738. }
  739.  
  740. /* ----------------- Init_print_rec() -------------------- */
  741. void
  742. Init_print_rec()
  743. {
  744.     short        save_resfile;
  745.     
  746.     save_resfile = CurResFile();
  747.     UseResFile( g_pref_resfile );
  748.     if (g_print_rec_h != NIL)
  749.         ReleaseResource( (Handle)g_print_rec_h );
  750.     g_print_rec_h = (THPrint) Get1Resource( 'Prec', 128 );
  751.     if (g_print_rec_h == nil)
  752.     {
  753.         fatal("Init_print_rec: error getting Prec resource.\r"
  754.             "Try deleting the DVIM72-Mac Prefs file.\r");
  755.     }
  756.     PrOpen();
  757.     Reset_resolution();
  758.     if (PrValidate( g_print_rec_h ))
  759.         Update_PREC();
  760.     PrClose();
  761.     UseResFile( save_resfile );
  762. }
  763.  
  764.  
  765. /* ------------------ Do_page_setup ----------------------- */
  766. void
  767. Do_page_setup()
  768. {
  769.     Init_print_rec();
  770.     PrOpen();
  771.     if (PrStlDialog( g_print_rec_h ))
  772.         Update_PREC();
  773.     PrClose();
  774. }
  775.  
  776. /* ----------------- Open_printer --------------------- */
  777.  
  778. void Open_printer( Boolean use_job_dialog,
  779.     int *first_page, int *last_page )
  780. {
  781.     char *s_ptr;
  782.     OSErr err;
  783.     
  784.     CHK("Open_printer");
  785.     UnloadSeg( sprintf );    /* get rid of ANSI segment */
  786.     UnloadSeg( special );    /* maybe we won't need it */
  787.     Init_print_rec();
  788.     CHK("Open_printer before PrOpen");
  789.     PrOpen();
  790.     g_print_status = 1;
  791.     CHK("Open_printer after PrOpen");
  792.     if (PrError() != noErr)
  793.     {
  794.         Show_error("Open_printer: PrOpen error.");
  795.         g_abort_dvi = TRUE;
  796.         Close_printer();
  797.         return;
  798.     }
  799.     g_print_res_file = CurResFile();
  800.     
  801.     if (get_bool_resource("MAX RESOLUTION"))
  802.         Maximize_resolution();
  803.     
  804.     if (use_job_dialog == USE_JOB_DIALOG)
  805.     {
  806.         err = Special_job_dialog( g_print_rec_h );
  807.         if (err != noErr)
  808.         {
  809.             g_abort_dvi = TRUE;
  810.             Close_printer();
  811.             return;
  812.         }
  813.         Update_PREC();
  814.     }
  815.     else
  816.     {
  817.         (**g_print_rec_h).prJob.iFstPage = 1;
  818.         (**g_print_rec_h).prJob.iLstPage = iPrPgMax;
  819.         (**g_print_rec_h).prJob.iCopies = 1;
  820.     }
  821.     
  822.     *first_page = (**g_print_rec_h).prJob.iFstPage;
  823.     *last_page = (**g_print_rec_h).prJob.iLstPage;
  824.     (**g_print_rec_h).prJob.iFstPage = 1;
  825.     (**g_print_rec_h).prJob.iLstPage = iPrPgMax;
  826.     
  827.     if (PrValidate(g_print_rec_h))
  828.         printf("PrValidate changed something!\n");
  829.  
  830.     UseResFile(g_print_res_file);
  831.     rollbeachball();
  832.     CHK("Open_printer before PrOpenDoc");
  833.     g_print_port_p = PrOpenDoc( g_print_rec_h, nil, nil );
  834.     CHK("Open_printer after PrOpenDoc");
  835.     if ((PrError() != noErr) || (g_print_port_p == NIL))
  836.     {
  837.         Show_error("Open_printer: PrOpenDoc error.");
  838.         g_abort_dvi = TRUE;
  839.         return;
  840.     }
  841.     g_print_status = 2;
  842.     g_printer_dpi = (**g_print_rec_h).prInfo.iVRes;
  843.     g_paper_width = ((**g_print_rec_h).prInfo.rPage.right -
  844.         (**g_print_rec_h).prInfo.rPage.left) * (long)g_dpi / g_printer_dpi;
  845.     g_paper_length = ((**g_print_rec_h).prInfo.rPage.bottom -
  846.         (**g_print_rec_h).prInfo.rPage.top) * (long)g_dpi / g_printer_dpi;
  847.     if (!g_draw_offscreen)
  848.         Set_pic_procs();
  849.     
  850.     if (g_draw_offscreen)
  851.     {
  852.         SetRect( &g_offscreen_GrafPort.portRect, 0, 0,
  853.             g_paper_width, g_paper_length );
  854.         g_offscreen_GrafPort.portBits.bounds = g_offscreen_GrafPort.portRect;
  855.         g_offscreen_GrafPort.portBits.rowBytes = ((g_paper_width + 31) >> 5) << 2;
  856.         g_offscreen_GrafPort.portBits.baseAddr =
  857.             MALLOC( g_offscreen_GrafPort.portBits.rowBytes
  858.                 * (long)g_paper_length );
  859.         if ( g_offscreen_GrafPort.portBits.baseAddr == NIL )
  860.         {
  861.             Show_error("Open_printer: Not enough memory for offscreen bitmap.");
  862.             g_abort_dvi = TRUE;
  863.             return;
  864.         }
  865.     }
  866.  
  867.     /* Set up the Cancel dialog */
  868.     if ((**g_print_rec_h).prJob.bJDocLoop == bSpoolLoop)
  869.         SetIText( cancel_dialog_text, "\pNow Spooling." );
  870.     else
  871.         SetIText( cancel_dialog_text, "\pNow Printing." );
  872.     REVEAL_DIALOG( g_cancel_dialog );
  873.     DrawDialog( g_cancel_dialog );
  874.     
  875.     Update_freemem();
  876.     CHK("Open_printer end");
  877. }
  878.  
  879. /* --------------------- User_wants_out -------------------- */
  880. Boolean
  881. User_wants_out( void )
  882. {
  883.     Printer_idle();
  884.     return( g_abort_dvi );
  885. }
  886.  
  887. /* ------------------- Printer_idle ---------------------- */
  888. pascal void
  889. Printer_idle(void )
  890. {
  891.     register  long        ticks;
  892.     GrafPtr    save_port;
  893.     
  894.     GetPort( &save_port );
  895.     rollbeachball();
  896.     ticks = TickCount();
  897.     if (Stack_space() < 5000L)
  898.     {
  899.         g_abort_dvi = TRUE;
  900.         PrSetError( iPrAbort );
  901.         printf( "\n[Sorry, getting low on stack space: %ld remains.]\n", Stack_space());
  902.     }
  903.     if (ticks - g_last_idle_time > IDLE_INTERVAL)
  904.     {
  905.         Update_memory_indicators();
  906.         Handle_event();
  907.         g_last_idle_time = ticks;
  908.     }
  909.     SetPort( save_port );
  910. }
  911.  
  912. /* --------------------- Close_printer -------------------- */
  913. /* Called by dvifile */
  914. void
  915. Close_printer()
  916. {
  917.     TPrStatus stat;
  918.     
  919.     UnloadSeg( sprintf );
  920.     UnloadSeg( special );
  921.     SetPort( FrontWindow() );
  922.     if (g_print_status == 3)
  923.     {
  924.         PrClosePage( g_print_port_p );
  925.         g_print_status = 2;
  926.     }
  927.     if (g_print_status >= 2)
  928.     {
  929.         PrCloseDoc( g_print_port_p ); 
  930.         g_print_status = 1;
  931.         if ( (PrError() != noErr) && (PrError() != iPrAbort) )
  932.         {
  933.             printf("\nPrCloseDoc error %d.\n", PrError());
  934.             /* (void)fflush(stdout); */
  935.         }
  936.         else
  937.             if ( ((**g_print_rec_h).prJob.bJDocLoop == bSpoolLoop)
  938.                 && (!g_abort_dvi) )
  939.             {
  940.                 SetIText( cancel_dialog_text, "\pNow Printing." );
  941.                 DrawDialog( g_cancel_dialog );
  942.                 (**g_print_rec_h).prJob.pIdleProc = (ProcPtr)Printer_idle;
  943.                 PrPicFile( g_print_rec_h, nil, nil, nil, &stat );
  944.                 if ( (PrError() != noErr) && (PrError() != iPrAbort) )
  945.                 {
  946.                     printf("\nPrPicFile error %d.\n", PrError());
  947.                     /* (void)fflush(stdout); */
  948.                 }
  949.             }
  950.         if (PrError() != noErr)
  951.         {
  952.             Explain_print_error( PrError() );
  953.         }
  954.     }
  955.     if (g_print_status == 1)
  956.     {
  957.         PrClose(); 
  958.         g_print_status = 0;
  959.     }
  960.     HideWindow( g_cancel_dialog );
  961.     SetPort ( FrontWindow() );
  962.     InitCursor();  /* in case of beach balls */
  963. }
  964.  
  965. #define    VERSION_ITEM    1
  966. #define    VERS_ID        1
  967.  
  968. typedef struct    vers {
  969.     unsigned char version_number;
  970.     unsigned char revision_number;
  971.     unsigned char revision_stage;
  972.     unsigned char build_number;
  973.     int    language_integer;
  974.     unsigned char string_data[];    /* two Pascal strings: abbreviated, GetInfo */
  975. } vers, **vers_h;
  976.  
  977. /* ----------------------- show_about() ---------------------------- */
  978. void
  979. show_about()
  980. {
  981.     DialogPtr    about_dlog;
  982.     short        type, hit;
  983.     Handle    stat_h;
  984.     Rect        box;
  985.     vers_h    v_h;
  986.     int    about_id = Get_resource_id('DLOG', "about box");
  987.     
  988.     about_dlog = GetNewDialog( about_id, nil, (WindowPtr)MOVE_TO_FRONT );
  989.     GetDItem( about_dlog, VERSION_ITEM,  &type, &stat_h, &box );
  990.     v_h = (vers_h) Get_resource_by_id('vers', VERS_ID);
  991.     SetIText( stat_h, (**v_h).string_data );
  992.     ShowWindow( about_dlog );
  993.     TD_activate( FALSE );
  994.     ModalDialog( nil, &hit );
  995.     DisposDialog( about_dlog );
  996.     ReleaseResource( (Handle)v_h );
  997. } /* show_about */
  998.  
  999. /* ---------------------- Kill_dvi ------------------------- */
  1000. //  Use this routine when we have to abort the printing job, but need not
  1001. //  bail out of the program entirely.
  1002. void Kill_dvi( char *msg )
  1003. {
  1004.     sillyfree();
  1005.     g_abort_dvi = true;
  1006.     if (g_print_status > 0)
  1007.         PrSetError( iPrAbort );
  1008.     printf("Printing aborted--");
  1009.     printf("%s\n", msg);
  1010.     printf( "Current TeX page counters: [%s]\n",tctos());
  1011.     Show_error( msg );
  1012. }
  1013.  
  1014. /* ---------------------- Show_error ------------------------- */
  1015. void
  1016. Show_error( msg )
  1017. char *msg;
  1018. {
  1019.     char pmsg[256];
  1020.     
  1021.     (void) strncpy( pmsg, msg, (size_t) 255 );
  1022.     pmsg[255] = '\0';
  1023.     (void) CtoPstr( pmsg );
  1024.     ParamText( (StringPtr)pmsg, "\p", "\p", "\p" );
  1025.     TD_activate(FALSE);
  1026.     StopAlert( Get_resource_id('ALRT', "error message"), nil );
  1027. }
  1028.  
  1029. /* ---------------------- Show_warning ------------------------- */
  1030. void
  1031. Show_warning( msg )
  1032. char *msg;
  1033. {
  1034.     char pmsg[256];
  1035.     
  1036.     (void) strncpy( pmsg, msg, (size_t) 255 );
  1037.     pmsg[255] = '\0';
  1038.     (void) CtoPstr( pmsg );
  1039.     ParamText( (StringPtr)pmsg, "\p", "\p", "\p" );
  1040.     TD_activate(FALSE);
  1041.     CautionAlert( Get_resource_id('ALRT', "warning message"), nil );
  1042. }
  1043.  
  1044. /* ------------------------ Get_resource_id -------------------- */
  1045. int
  1046. Get_resource_id(
  1047.     OSType        resource_type,
  1048.     char        *resource_name)
  1049. {
  1050.     char rsrc_name[80], errmsg[150];
  1051.     Handle resource_handle;
  1052.     short        resource_id;
  1053.  
  1054.     (void) strcpy( rsrc_name, resource_name );
  1055.     (void) CtoPstr( rsrc_name );
  1056.     resource_handle = GetNamedResource( resource_type, (StringPtr)rsrc_name );
  1057.     if (resource_handle == nil)
  1058.     {
  1059.         SysBeep(1);
  1060.         sprintf( errmsg, "Get_resource_id error getting '%4.4s' '%s'",
  1061.             (char *)&resource_type, resource_name );
  1062.         fatal(errmsg);
  1063.     };
  1064.     GetResInfo( resource_handle, &resource_id, nil, nil );
  1065.     return( resource_id );
  1066. }
  1067.  
  1068. /* ------------------------ Get_resource ----------------------------- */
  1069. void
  1070. Get_resource( /* find a resource by name (in the preferences file)*/
  1071.     OSType    resource_type,        /* given type... */
  1072.     char        *resource_name,    /* and name... */
  1073.     Handle    *resource_handle,    /* it returns handle... */
  1074.     short        *resource_id        /* and id. */
  1075.     )
  1076. {
  1077.     char rsrc_name[255];
  1078.     char temp[5];
  1079.     int    save_resfile;
  1080.  
  1081.     (void) strcpy( rsrc_name, resource_name );
  1082.     (void) CtoPstr( rsrc_name );
  1083.     save_resfile = CurResFile();
  1084.     UseResFile( g_pref_resfile );
  1085.     *resource_handle = Get1NamedResource( resource_type, (StringPtr)rsrc_name );
  1086.     UseResFile( save_resfile );
  1087.     if (*resource_handle == nil)
  1088.     {
  1089.         (void) strncpy( temp, (char *)&resource_type, 4 );
  1090.         temp[4] = '\0';
  1091.         (void) sprintf( rsrc_name,
  1092.             "Couldn't find resource '%s' of type '%s'!\r"
  1093.             "Try deleting the DVIM72-Mac Prefs file.\r",
  1094.             resource_name, temp );
  1095.         fatal(rsrc_name);
  1096.     };
  1097.     GetResInfo( *resource_handle, resource_id, nil, nil );
  1098. } /* Get_resource */
  1099.  
  1100. /* --------------------- Get_resource_by_id ---------------------- */
  1101. Handle
  1102. Get_resource_by_id( /* find a resource by ID (in the application) */
  1103.     OSType    resource_type,        /* given type... */
  1104.     int        resource_id        /* and id. */
  1105.     )
  1106. {
  1107.     char rsrc_name[80];
  1108.     char temp[5];
  1109.     Handle    resource_handle;
  1110.     int    save_resfile;
  1111.  
  1112.     save_resfile = CurResFile();
  1113.     UseResFile( g_app_resfile );
  1114.     resource_handle = Get1Resource( resource_type, resource_id );
  1115.     UseResFile( save_resfile );
  1116.     if (resource_handle == nil)
  1117.     {
  1118.         (void) strncpy( temp, (char *)&resource_type, 4 );
  1119.         temp[4] = '\0';
  1120.         (void) sprintf( rsrc_name,
  1121.             "Couldn't find resource %d of type '%s'!",
  1122.             resource_id, temp );
  1123.         fatal(rsrc_name);
  1124.     };
  1125.     return( resource_handle );
  1126. } /* Get_resource_by_id */
  1127.  
  1128. /* ------------------------- init_toolbox --------------------------- */
  1129. void init_toolbox()
  1130. {
  1131.     long extra_stack;
  1132.     Ptr p;
  1133.     long    **stack_space;
  1134.     
  1135.     stack_space = (long **) Get1NamedResource( 'LONG', "\pstack space" );
  1136.     if (stack_space != NIL)
  1137.     {
  1138.         extra_stack = **stack_space - Stack_space();
  1139.         if (extra_stack > 0)
  1140.         {
  1141.             SetApplLimit( GetApplLimit() - extra_stack );
  1142.         }
  1143.         ReleaseResource( (Handle) stack_space );
  1144.     }
  1145.     MaxApplZone();
  1146.     MoreMasters(); MoreMasters(); MoreMasters();
  1147.     InitGraf( &thePort );
  1148.     InitFonts();
  1149.     FlushEvents(everyEvent, 0);
  1150.     InitWindows();
  1151.     InitMenus();
  1152.     TEInit();
  1153.     InitDialogs(0L);
  1154.     InitCursor();
  1155. #if PROFILE
  1156.     InitProfile( 300, 300 );
  1157. #endif
  1158. } /* init_toolbox */
  1159.  
  1160. /* ---------------------- set_str_resource ------------------------- */
  1161. void
  1162. set_str_resource( char *rsrc_name, char *new_value )
  1163. {
  1164.     char    **r_handle;
  1165.     short     rsrc_id, new_len, i;
  1166.     
  1167.     /* Get a handle to the resource. */
  1168.     Get_resource( 'Cstr', rsrc_name, &r_handle, &rsrc_id );
  1169.     
  1170.     new_len = strlen( new_value );
  1171.     
  1172.     /* Resize the handle. */
  1173.     if (new_len + 1 != GetHandleSize( r_handle ))
  1174.         SetHandleSize( r_handle, new_len + 1 );
  1175.     if (MemError() != noErr)
  1176.         Show_warning("set_str_resource: SetHandleSize error");
  1177.     
  1178.     /* Copy the new value to the new handle. */
  1179.     (void) strcpy( *r_handle, new_value );
  1180.     
  1181.     /* Update the resource file. */
  1182.     ChangedResource( r_handle );
  1183.     if (ResError() != noErr)
  1184.         Show_warning("set_str_resource: ChangedResource error");
  1185.     WriteResource( r_handle );
  1186.     if (ResError() != noErr)
  1187.         Show_warning("set_str_resource: WriteResource error");
  1188.     UpdateResFile( g_pref_resfile );
  1189.     (void) FlushVol( NIL, g_pref_vRefNum );
  1190. } /* set_str_resource */
  1191.  
  1192. /* ------------------------ Update_PREC ------------------------ */
  1193. void Update_PREC( void )
  1194. {
  1195.     ChangedResource( (Handle) g_print_rec_h );
  1196.     UpdateResFile( g_pref_resfile );
  1197.     (void) FlushVol( NIL, g_pref_vRefNum );
  1198. }
  1199.  
  1200. /* ---------------------- set_int_resource ------------------------- */
  1201. void
  1202. set_int_resource( char *rsrc_name, int new_value )
  1203. {
  1204.     int    **r_handle;
  1205.     short     rsrc_id, new_len, i;
  1206.     short        save_resfile;
  1207.  
  1208.     save_resfile = CurResFile();
  1209.     UseResFile( g_pref_resfile );
  1210.  
  1211.     /* Get a handle to the resource. */
  1212.     Get_resource( 'int ', rsrc_name, (Handle *)&r_handle, &rsrc_id );
  1213.     
  1214.     /* Copy the new value to the new handle. */
  1215.     **r_handle = new_value;
  1216.     
  1217.     /* Update the resource file. */
  1218.     ChangedResource( (Handle)r_handle );
  1219.     if (ResError() != noErr)
  1220.         Show_warning("set_int_resource: ChangedResource error");
  1221.     WriteResource( (Handle)r_handle );
  1222.     if (ResError() != noErr)
  1223.         Show_warning("set_int_resource: WriteResource error");
  1224.     UpdateResFile( g_pref_resfile );
  1225.     (void) FlushVol( NIL, g_pref_vRefNum );
  1226. } /* set_int_resource */
  1227.  
  1228.  
  1229. /* ---------------------- get_bool_resource ----------------- */
  1230. Boolean
  1231. get_bool_resource( char *rsrc_name )
  1232. {
  1233.     Handle r_handle;
  1234.     short        rsrc_id;
  1235.  
  1236.     Get_resource( 'Bool', rsrc_name, &r_handle, &rsrc_id );
  1237.     return( **(Boolean **)r_handle );
  1238. } /* get_bool_resource */
  1239.  
  1240. /* ---------------------- get_int_resource ----------------- */
  1241. int
  1242. get_int_resource( char *rsrc_name )
  1243. {
  1244.     Handle r_handle;
  1245.     short        rsrc_id;
  1246.     
  1247.     Get_resource( 'int ', rsrc_name, &r_handle, &rsrc_id );
  1248.     return( **(int **)r_handle );
  1249. } /* get_int_resource */
  1250.  
  1251. /*----------------------- get_str_resource -------------------------*/
  1252. char **
  1253. get_str_resource( char *rsrc_name )
  1254. {
  1255.     Handle r_handle;
  1256.     short    rsrc_id;
  1257.     
  1258.     Get_resource( 'Cstr', rsrc_name, &r_handle, &rsrc_id );
  1259.     return( (char **) r_handle );
  1260. } /* get_str_resource */
  1261.  
  1262. /* ---------------------- Get_SF_place ----------------------- */
  1263. Point Get_SF_place( short rsrc_id )
  1264. {
  1265.     Point where;
  1266.     DialogTHndl sfget_h;
  1267.     int sfget_width, sfget_depth;
  1268.  
  1269. /* Figure out how to center the SFGetFile dialog on the screen.*/
  1270.     sfget_h = (DialogTHndl) GetResource( 'DLOG', rsrc_id );
  1271.     if (ResError())
  1272.         Show_warning("SFGet resource error 1.");
  1273.     LoadResource( (Handle)sfget_h );
  1274.     if (ResError())
  1275.         Show_warning("SFGet resource error 2.");
  1276.     sfget_width = (**sfget_h).boundsRect.right
  1277.         - (**sfget_h).boundsRect.left;
  1278.     sfget_depth = (**sfget_h).boundsRect.bottom
  1279.         - (**sfget_h).boundsRect.top;
  1280.     where.h = (g_screenrect.right
  1281.         - g_screenrect.left - sfget_width) / 2
  1282.         + g_screenrect.left;
  1283.     where.v = (g_screenrect.bottom
  1284.         - g_screenrect.top - sfget_depth) / 3
  1285.         + g_screenrect.top;
  1286.     return( where );
  1287. }
  1288.  
  1289. /* ------------------------- Get_file_path -------------------------*/
  1290. void
  1291. Get_file_path( char *thepath )
  1292. {
  1293. Point    where;
  1294. SFReply reply;
  1295. int errcode;
  1296.  
  1297.     where = Get_SF_place( getDlgID );    
  1298.  
  1299. /* Put up standard file dialog box for the user to pick a file.  Show
  1300. only files of type ODVI or with names ending in ".dvi" or ".DVI" */
  1301.  
  1302.     SFGetFile(where, (StringPtr)"\pfile:", (ProcPtr)Dvi_filter, -1, nil, 0L, &reply);
  1303.     
  1304.     if (reply.good)
  1305.     {
  1306.         (void) PtoCstr( reply.fName );
  1307.         (void) strcpy( thepath, (char *)reply.fName );
  1308.         errcode = SetVol( nil, reply.vRefNum );
  1309.         if (errcode != noErr)
  1310.             printf("SetVol error %d.\n", errcode );
  1311.     }
  1312.     else
  1313.     {
  1314.         (void)strcpy( thepath, "" );
  1315.     }
  1316. } /* Get_file_path */
  1317.  
  1318.  
  1319. void
  1320. init_per_dvi()            /* initialize global variables */
  1321. {
  1322.     register INT16 i;        /* loop index */
  1323.  
  1324. /***********************************************************************
  1325.     Set up masks such that rightones[k]  has 1-bits at the right end  of
  1326.     the word from k ..     HOST_WORD_SIZE-1, where bits are numbered  from
  1327.     left (high-order) to right (lower-order), 0 .. HOST_WORD_SIZE-1.
  1328.  
  1329.     img_mask[k] has  a 1-bit  in  position k,  bits numbered  as  above.
  1330.     power[k] is  1  <<  k,  and gpower[k]  is  2**k-1  (i.e.  1-bits  in
  1331.     low-order k positions).  These 3 require only 32 entries each  since
  1332.     they deal with 32-bit words from the PK and GF font files.
  1333.  
  1334.     These are set  at run-time, rather  than compile time  to avoid  (a)
  1335.     problems with C  preprocessors which sometimes  do not handle  large
  1336.     constants correctly, and (b) host byte-order dependence.
  1337. ***********************************************************************/
  1338.  
  1339.  
  1340.     debug_code = 0;
  1341.  
  1342.     npage = 0;
  1343.     topmargin = 1.0;            /* DVI driver standard default */
  1344.     leftmargin = 1.0;            /* DVI driver standard default */
  1345.  
  1346.     subfile[0] = '\0';
  1347.  
  1348. #if    VIRTUAL_FONTS
  1349.     virt_font = FALSE;
  1350. #endif
  1351.  
  1352. }
  1353.